<?php
/**
 * Created by PhpStorm.
 * User: yiyu
 * Email: smalleasyu@gmail.com
 * Date: 2020-02-01 20:23:20
 * Desc: 统一处理app内的分享信息
 */

namespace app\api\service;

use app\base\service\BaseService;
use app\base\model\system\SystemConfig;
use app\base\model\user\User;
use app\base\model\goods\GoodsBaseInfo;
use app\base\service\WeChat\MiniProgramQrCodeService;
use app\core\log\Log4dd;

class ShareService extends BaseService
{

    public static function miniProgramShareWish($group_id, $goods_id, $user_id)
    {

        $user_info = User::getUserInfoById($user_id);
        $refer_code = $user_info['refer_code'] ?? '';
        $gender = $user_info['gender'] ?? 2;//默认使用女性

        $goods_info = GoodsBaseInfo::getGoodsBaseInfo($goods_id);

        $mini_path = SystemConfig::getSystemConfigByName('support_success_share_friend_mini_path', '');

        if ($mini_path) {
            $mini_path = str_replace('{group_id}', $group_id, $mini_path);
            $mini_path = str_replace('{refer_code}', $refer_code, $mini_path);
        }

        $wx_friend_share_info = [
            'share_title' => SystemConfig::getSystemConfigByName('support_success_share_friend_txt', ''),
            'share_img' => SystemConfig::getSystemConfigByName('support_success_share_friend_img', ''),
            'path' => $mini_path,
        ];

        //获取海报底图
        if ($gender == 1) {
            $poster_url = SystemConfig::getSystemConfigByName('support_success_share_circle_man_poster_url', '');
            $qr_code_xyw = SystemConfig::getSystemConfigByName('support_success_share_circle_man_poster_xyw', '0*0*0');
        } else {
            $poster_url = SystemConfig::getSystemConfigByName('support_success_share_circle_woman_poster_url', '');
            $qr_code_xyw = SystemConfig::getSystemConfigByName('support_success_share_circle_woman_poster_xyw', '0*0*0');
        }

        list($x, $y, $width) = explode('*', $qr_code_xyw);

        $func_name = 'shareWish';

        $params = compact('user_id', 'goods_id', 'group_id', 'refer_code', 'func_name');

        $wx_circle_share_info = [
            'qr_code_url' => MiniProgramQrCodeService::getUnLimitMiniQrCode($mini_path, $params, ['width' => $width, 'is_hyaline' => true]),
            'poster_url' => $poster_url,
            'x_coordinate' => $x,
            'y_coordinate' => $y,
            'nick_name' => $user_info['nick_name'] ?? '',
            'avatar_url' => $user_info['small_avatar_url'] ?? '',
            'goods_name' => $goods_info['goods_name'] ?? '',
            'goods_title' => $goods_info['goods_title'] ?? '',
            'goods_img_url' => $goods_info['goods_img_url'] ?? '',
            'title' => '我的好物心愿需要你的支持'
        ];

        Log4dd::info("生成小程序二维码最终数据", $wx_circle_share_info);

        empty($wx_circle_share_info['qr_code_url']) && $wx_circle_share_info['qr_code_url'] = 'https://cdn.zhixianggo.com/config/4a55babab7acc757735ca2f2ebd7ccbe.png';

        return [
            'wx_friend_share_info' => $wx_friend_share_info,
            'wx_circle_share_info' => $wx_circle_share_info
        ];


    }


}